﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Data;

using System.Text;
using System.Windows.Forms;
using ITAP.ShprossesBuilder.Modelling;
using ITAP.ShprossesBuilder.Modelling.Components;

namespace ITAP.ShprossesBuilder
{
    public partial class ModelTreeView : TreeView, IModelVisitor
    {
      //  private int modelContainerPreviousAllInnerComponentsCount = 0;
        private TreeNode prevNode;
        private MainControl mainControl;

        public MainControl MainControl
        {
            get { return mainControl; }
            set { mainControl = value; }
        }

        public ModelTreeView()
        {
            InitializeComponent();
        }

        protected override void OnBeforeCheck(TreeViewCancelEventArgs e)
        {
            base.OnBeforeCheck(e);
        }

        private bool enableVisitor = true;

        protected override void OnAfterCheck(TreeViewEventArgs e)
        {
            if (!this.enableVisitor)
                return;

            if (this.mainControl != null)
            {
                if (e.Node.Tag != null)
                {
                    ((IComponent)e.Node.Tag).Selected = e.Node.Checked;
                    if (e.Node.Checked)
                    {
                        if(!this.mainControl.ModelContainer.SelectedComponentsStack.Contains((IComponent)e.Node.Tag))
                            this.mainControl.ModelContainer.SelectedComponentsStack.Add((IComponent)e.Node.Tag);
                    }
                    else
                        this.mainControl.ModelContainer.SelectedComponentsStack.Remove((IComponent)e.Node.Tag);

                        enableVisitor = false;
                    this.mainControl.Manipulator.UpdateAllVisitors();
                    enableVisitor = true;
                }
                base.OnAfterCheck(e);
            }
        }

        #region IModelVisitor Members

        public void Visit(ITAP.ShprossesBuilder.Modelling.Components.Model model)
        {
            if (!this.enableVisitor)
                return;

            TreeNode currentNode = new TreeNode(model.Name);            
            currentNode.Tag = model;
            prevNode.Nodes.Add(currentNode);

            prevNode = currentNode;
            foreach (IComponent component in model.InnerComponents)
            {
                component.AcceptVisitor(this);
                prevNode = currentNode;
            }
        }

        public void Visit(ITAP.ShprossesBuilder.Modelling.Components.Connector connector)
        {
            return;

            TreeNode currentNode = new TreeNode(connector.GetType().Name);
            currentNode.Tag = connector;
            prevNode.Nodes.Add(currentNode);

            if (connector.Selected)
                currentNode.Checked = true;
            else
                currentNode.Checked = false;


            prevNode = currentNode;
            foreach (IComponent component in connector.InnerComponents)
            {
                component.AcceptVisitor(this);
                prevNode = currentNode;
            }
        }

        public void Visit(ITAP.ShprossesBuilder.Modelling.Components.Shpross shpross)
        {
            if (!this.enableVisitor)
                return;

            TreeNode currentNode = new TreeNode(shpross.Name + " " + shpross.SerialNumber);
            currentNode.Tag = shpross;
            prevNode.Nodes.Add(currentNode);

            if (shpross.Selected)
                currentNode.Checked = true;
            else
                currentNode.Checked = false;


            prevNode = currentNode;
            foreach (IComponent component in shpross.InnerComponents)
            {
                component.AcceptVisitor(this);
                prevNode = currentNode;
            }
        }

        public void Visit(ITAP.ShprossesBuilder.Modelling.Components.Spacer spacer)
        {
            return;
            TreeNode currentNode = new TreeNode(spacer.GetType().Name);
            currentNode.Tag = spacer;
            prevNode.Nodes.Add(currentNode);

            if (spacer.Selected)
                currentNode.Checked = true;
            else
                currentNode.Checked = false;

            prevNode = currentNode;
            foreach (IComponent component in spacer.InnerComponents)
            {
                component.AcceptVisitor(this);
                prevNode = currentNode;
            }
        }

        public void Visit(ITAP.ShprossesBuilder.Modelling.Components.ShprossConnectorStub stub)
        {
            return;
            TreeNode currentNode = new TreeNode(stub.GetType().Name);
            currentNode.Tag = stub;
            prevNode.Nodes.Add(currentNode);

            if (stub.Selected)
                currentNode.Checked = true;
            else
                currentNode.Checked = false;

            prevNode = currentNode;
            foreach (IComponent component in stub.InnerComponents)
            {
                component.AcceptVisitor(this);
                prevNode = currentNode;
            }
        }

        public void Visit(ITAP.ShprossesBuilder.Modelling.Components.ShprossConnector shprossConnector)
        {
            if (!this.enableVisitor)
                return;

            TreeNode currentNode = new TreeNode(shprossConnector.Name + " " + shprossConnector.SerialNumber.ToString());
            currentNode.Tag = shprossConnector;
            prevNode.Nodes.Add(currentNode);

            if (shprossConnector.Selected)
                currentNode.Checked = true;
            else
                currentNode.Checked = false;


            prevNode = currentNode;
            foreach (IComponent component in shprossConnector.InnerComponents)
            {
                component.AcceptVisitor(this);
                prevNode = currentNode;
            }
        }

        public void Visit(ITAP.ShprossesBuilder.Modelling.Components.SpacerElement spacerElement)
        {
            return;
            TreeNode currentNode = new TreeNode(spacerElement.GetType().Name);
            currentNode.Tag = spacerElement;
            prevNode.Nodes.Add(currentNode);

            if (spacerElement.Selected)
                currentNode.Checked = true;
            else
                currentNode.Checked = false;


            prevNode = currentNode;
            foreach (IComponent component in spacerElement.InnerComponents)
            {
                component.AcceptVisitor(this);
                prevNode = currentNode;
            }
        }

        public void Visit(ITAP.ShprossesBuilder.Modelling.Components.SpacerConnector spacerConnector)
        {
            return;
            TreeNode currentNode = new TreeNode(spacerConnector.GetType().Name);
            currentNode.Tag = spacerConnector;
            prevNode.Nodes.Add(currentNode);

            if (spacerConnector.Selected)
                currentNode.Checked = true;
            else
                currentNode.Checked = false;


            prevNode = currentNode;
            foreach (IComponent component in spacerConnector.InnerComponents)
            {
                component.AcceptVisitor(this);
                prevNode = currentNode;
            }
        }

        public void Visit(ITAP.ShprossesBuilder.Modelling.Components.ModelCell modelCell)
        {
            if (!this.enableVisitor)
                return;

            TreeNode currentNode = new TreeNode(modelCell.Name);
          
            currentNode.Tag = modelCell;
            prevNode.Nodes.Add(currentNode);

            if (modelCell.Selected)
                currentNode.Checked = true;
            else
                currentNode.Checked = false;


            prevNode = currentNode;
            foreach (IComponent component in modelCell.InnerComponents)
            {
                component.AcceptVisitor(this);
                prevNode = currentNode;
            }
        }

        public void Visit(ITAP.ShprossesBuilder.Modelling.Components.ModelContainer modelContainer)
        {
            if (!this.enableVisitor)
                return;

            this.BeginUpdate();
            TreeNode currentNode;
            //int currentInnerComponentsCount = modelContainer.AllInnerComponents.Count;
            //if (currentInnerComponentsCount != modelContainerPreviousAllInnerComponentsCount)
            //{
                this.Nodes.Clear();
            //    modelContainerPreviousAllInnerComponentsCount = currentInnerComponentsCount;

                currentNode = new TreeNode("Контейнер");
                currentNode.Tag = modelContainer;
                this.Nodes.Add(currentNode);
            //}
            //else
            //{
            //    currentNode = this.Nodes[0];
            //}
                if (modelContainer.Selected)
                    currentNode.Checked = true;
                else
                    currentNode.Checked = false;
//                this.SelectedNode = currentNode;

            prevNode = currentNode;
            foreach (IComponent component in modelContainer.InnerComponents)
            {                   
                component.AcceptVisitor(this);
                prevNode = currentNode;
            }
            this.ExpandAll();
            this.EndUpdate();
            this.Refresh();
        }

        #endregion

        private void ModelTreeView_Leave(object sender, EventArgs e)
        {
            
        }

        private void ModelTreeView_Enter(object sender, EventArgs e)
        {
            //if (this.SelectedNode != null)
            //    this.SelectedNode.BackColor = Color.Green;
        }

        private void ModelTreeView_BeforeSelect(object sender, TreeViewCancelEventArgs e)
        {
            //e.Node.BackColor = Color.Green;
            //if (this.SelectedNode != null)
            //    this.SelectedNode.BackColor = Color.White;
        }

        #region IModelVisitor Members


        public void Visit(StickConnector stickConnector)
        {
//            return;

            TreeNode currentNode = prevNode; // new TreeNode(stickConnector.Name);
            //currentNode.Tag = stickConnector;
            //prevNode.Nodes.Add(currentNode);

            //if (stickConnector.Selected)
            //    currentNode.Checked = true;
            //else
            //    currentNode.Checked = false;


           // prevNode = currentNode;
            foreach (IComponent component in stickConnector.InnerComponents)
            {
                component.AcceptVisitor(this);
                prevNode = currentNode;
            }
            //throw new NotImplementedException();
        }

        #endregion

        #region IModelVisitor Members


        public void Visit(SnapOnCap snapOnCap)
        {
            TreeNode currentNode = new TreeNode(snapOnCap.Name);
            currentNode.Tag = snapOnCap;
            prevNode.Nodes.Add(currentNode);

            if (snapOnCap.Selected)
                currentNode.Checked = true;
            else
                currentNode.Checked = false;


            prevNode = currentNode;
            //foreach (IComponent component in stickConnector.InnerComponents)
            //{
            //    component.AcceptVisitor(this);
            //    prevNode = currentNode;
            //}
        }

        #endregion

        #region IModelVisitor Members


        public void Visit(ShprossEndToEndConnector endToEndConnector)
        {
          //  throw new NotImplementedException();
        }

        #endregion

        #region IModelVisitor Members


        public void Visit(ShprossChain shprossChain)
        {
       //     throw new NotImplementedException();
        }

        #endregion

        #region IModelVisitor Members


        public void Visit(SpacerContainer spacerContainer)
        {
           // throw new NotImplementedException();
        }

        #endregion

        #region IModelVisitor Members


        public void Visit(CrossConnector crossConnector)
        {
       //     throw new NotImplementedException();
        }

        #endregion
    }
}
